﻿namespace WriterStars.Controllers
{
    using System;
    using System.Linq;
    using System.Net;
    using System.Web.Mvc;

    using Microsoft.AspNet.Identity;

    using WriterStars.Model;
    using WriterStars.Models.ViewModels;
    using WriterStars.Infrastructure.Services;
    using WriterStars.Data;
    using System.Collections.Generic;
    [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        private IWriterStarsData data;


        public AdminController()
        {
            this.data = new WriterStarsData(new WriterStarsDbContext());
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Books()
        {


            return this.View(new List<AuthorViewModel>());
        }

        [HttpPost]
        public ActionResult ChangeCategory(int id, int category)
        {
            var book = this.data.Books.GetById(id);
            if (book == null)
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);

            book.CategoryId = category;

            this.data.Books.Update(book);
            this.data.Books.SaveChanges();

            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }
        

        public ActionResult Users()
        {
            var users = this.data.Users.GetAll().ToList();

            return this.View(users);
        }

        public ActionResult Categories()
        {
            var categories = this.data.Categories.GetAll().ToList();
            return this.View(categories);
        }

        [HttpPost]
        public ActionResult Categories(Category category)
        {
            if (category == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            if (ModelState.IsValid)
            {
                this.data.Categories.Create(category);
                this.data.Categories.SaveChanges();
            }

            var categories = this.data.Categories.GetAll().ToList();

            return this.PartialView("_Categories", categories);
        }

        public ActionResult Contest()
        {
            var currentUserId = this.User.Identity.GetUserId();
            var contests = this.data.Contests.GetAll().ToList().Select(x => new ContestViewModel(x)).ToList();
            //contests.Add(new ContestViewModel(new Contest
            //{
            //    Name = "Writer Wars",
            //    Regulations = "This is some shitty ass content",
            //    StartDate = DateTime.Now.AddDays(-5),
            //    EndDate = DateTime.Now.AddDays(-1)
            //}));

            return this.View(contests);
        }

        [HttpPost]
        public ActionResult Contest(Contest contest)
        {
            this.data.Contests.Create(contest);
            var contests = this.data.Contests.GetAll().OrderByDescending(x => x.StartDate).ToList();


            var contestsViewModel = contests.Select(x => new ContestViewModel(x));
            return this.PartialView("_Contests", contestsViewModel);
        }

        public ActionResult Orders()
        {
            var order = this.data.Orders.GetIncluding(x => x.User).ToList();

            return this.View(order);
        }
    }
}